// pages/lishi/lishi.js

var api = require('../../js/api.js')
var utilBT = require('../../js/utilBT.js');

import * as echarts from '../../ec-canvas/echarts';

const app = getApp();

let olddata = [];
let newdata = [];

let ddate = '';
let dwendu = '';

let min = 0;
let max = 255;


let start = '2020-07-30'
let end = '2020-07-31'
var loadingtime1 = ''
var loadingtime2 = ''


function initChart(canvas, width, height) {
    console.log('====>>开始初始化')
    console.log(canvas)
    console.log(width)
    console.log(height)

    const chart = echarts.init(canvas, null, {
        width: width,
        height: height,


    });
    canvas.setChart(chart);

    var option = {
        title: {
            text: '心率'
        },


        tooltip: {
            trigger: 'axis',
            // triggerOn: 'click',
            // position: function (point, params, dom, rect, size) {
            //     console.log('===>>点信息')
            //     console.log(params)
            //     console.log(params[0].value)
            //     ddate = params[0].value[0]
            //     dwendu = params[0].value[1]
            //     return [200, 0];
            // },
            position: function (point, params, dom, rect, size) {
                // 鼠标坐标和提示框位置的参考坐标系是：以外层div的左上角那一点为原点，x轴向右，y轴向下
                // 提示框位置
                var x = 0; // x坐标位置
                var y = 0; // y坐标位置

                // 当前鼠标位置
                var pointX = point[0];
                var pointY = point[1];

                // 外层div大小
                // var viewWidth = size.viewSize[0];
                // var viewHeight = size.viewSize[1];

                // 提示框大小
                var boxWidth = size.contentSize[0];
                var boxHeight = size.contentSize[1];

                // boxWidth > pointX 说明鼠标左边放不下提示框
                if (boxWidth > pointX) {
                    x = 5;
                } else { // 左边放的下
                    x = pointX - boxWidth;
                }

                // boxHeight > pointY 说明鼠标上边放不下提示框
                if (boxHeight > pointY) {
                    y = 5;
                } else { // 上边放得下
                    y = pointY - boxHeight;
                }

                return [x, y];
            },
            formatter(params) {
                const item = params[0];
                return `时间：${item.value[2]}\n心率：${item.value[1]}`;
            },
        },
        xAxis: {
            type: 'value',
            show: false,

            // name: '时间',

            // boundaryGap: false,
            axisTick: {
                inside: true
            },

        },
        yAxis: {
            // name: '体温',
            type: 'value',
            minInterval: 1, //坐标间隔大小

            min: 0,
            max: 255,
            minorTick: {
                show: true, //是否显示次刻度线。

            },

            axisTick: {
                intervar: 1, //坐标轴刻度的显示间隔
                inside: true, //坐标轴刻度是否朝内

            },


        },

        dataZoom: [{
            id: 'dataZoomX',
            type: 'slider',
            xAxisIndex: [0],
            filterMode: 'filter',
            orient: "horizontal",
            // startValue: start,
            zoomLock: false,

        },
            {
                id: 'dataZoomY',
                show: false,

                type: 'inside',
                yAxisIndex: [0],
                filterMode: 'empty',
                zoomLock: true,

            }
        ],

        series: {
            // name: '心率',

            type: 'line',
            data: [],
            symbol: 'none',  //取消折点圆圈


        }
    }

    loadingtime2 = setInterval(function () {
        if (olddata != newdata) {
            olddata = newdata
            console.log('===========>>>>>>>>>>old xxxxxxxx new')
            console.log(olddata)

            getDateValue(olddata)
            // if (app.globalData.cels == true) {
            //     option.yAxis.min = 34
            //     option.yAxis.max = 43
            // } else {
            //     option.yAxis.min = 93
            //     option.yAxis.max = 110
            // }
            option.yAxis.min = min
            option.yAxis.max = max
            option.series.data = olddata
            option.dataZoom[0].startValue = start
            option.dataZoom[0].endValue = end

            chart.setOption(option);
        } else {
            // console.log('===========>>>>>>>>>>old===new')
        }


    }, 1000)

    return chart;
}


function getDateValue(data) {
    console.log('===>>判断开始和结束时间')
    console.log(data)
    if (data.length == 0) {
        end = 0
        start = 0
    } else if (data.length <= 500) {
        end = data[data.length - 1][0]
        start = data[0][0]
    } else if (data.length > 500) {
        end = data[data.length - 1][0]
        start = data[data.length - 501][0]
    }
    console.log(start)
    console.log(end)
}


Page({

    /**
     * 页面的初始数据
     */
    data: {
        date: '2020-07-30',
        startDate: '2020-01-01',
        endDate: '2025-12-31',
        ec: {
            onInit: initChart,
        },
        min_heart: 75,
        avg_heart: 90,
        max_heart: 180,

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        let date = utilBT.formatDate(new Date())

        var that = this
        that.setData({
            date: date
        })

        if (!app.globalData.head.noToken) {
            that.findEcgs(that.data.date)
        }

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        // util.checkToken()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {


    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.findEcgs(this.data.date)
        olddata = []

        // 处理完成后，终止下拉刷新
        wx.stopPullDownRefresh()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    bindPickerChangeDate: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            date: e.detail.value
        })
        this.findEcgs(this.data.date)
    },
    findEcgs: function (date) {
        var that = this
        api.findEcgsByDate(date, function (res) {
            newdata = res.ecgs
            min = res.minValue - 5
            max = res.maxValue + 5
            that.setData({
                min_heart: res.minValue,
                avg_heart: res.avgValue,
                max_heart: res.maxValue,
            })

        })
    }
})
